"""
@Time ： 2021/11/11 14:38
@Auth ： ChenZhangKun
"""
from selenium import webdriver
from selenium.webdriver.common.by import By


def switch_window(wd, windName):
    # 拿到全部windows
    handles = wd.window_handles
    for handle in handles:
        wd.switch_to.window(handle)
        if windName in wd.title:
            break


wd = webdriver.Chrome(r'D:\sofwares\chrom\chromedriver.exe')
wd.implicitly_wait(10)

wd.get('http://cdn1.python3.vip/files/selenium/sample3.html')
# 保存当前窗口的句柄
currentWindow = wd.current_window_handle

element = wd.find_element(By.TAG_NAME, "a")
# 点击->跳转另一标签页
element.click()
# 切换窗口方法
switch_window(wd, "必应")
print(wd.title)

# 返回现在的窗口
wd.switch_to.window(currentWindow)
print(wd.title)